<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键词" prop="serveTypeName">
        <el-input v-model="queryParams.serveTypeName" placeholder="请输入关键词" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="服务地址" prop="serveAddress">
        <el-input v-model="queryParams.serveAddress" placeholder="请输入服务地址" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="预约时间">
        <el-date-picker v-model="daterangeServeStartTime" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="seizeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="id" />
      <el-table-column label="服务类型" align="center" prop="serveItemName" />
      <el-table-column label="关键词" align="center" prop="serveTypeName" />
      <el-table-column label="序号" align="center" prop="serveItemId" />
      <el-table-column label="服务地址" align="center" prop="serveAddress" />
      <el-table-column label="预约时间" align="center" prop="serveStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serveStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务数量" align="center" prop="purNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="grabOrders(scope.row)">抢单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改抢单池对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="序号" prop="serveTypeId">
          <el-input v-model="form.serveTypeId" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serveItemName">
          <el-input v-model="form.serveItemName" placeholder="请输入服务类型" />
        </el-form-item>
        <el-form-item label="关键词" prop="serveTypeName">
          <el-input v-model="form.serveTypeName" placeholder="请输入关键词" />
        </el-form-item>
        <el-form-item label="序号" prop="serveItemId">
          <el-input v-model="form.serveItemId" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="服务地址" prop="serveAddress">
          <el-input v-model="form.serveAddress" placeholder="请输入服务地址" />
        </el-form-item>
        <el-form-item label="服务项目图片" prop="serveItemImg">
          <el-input v-model="form.serveItemImg" placeholder="请输入服务项目图片" />
        </el-form-item>
        <el-form-item label="订单总金额" prop="ordersAmount">
          <el-input v-model="form.ordersAmount" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="预约时间" prop="serveStartTime">
          <el-date-picker clearable v-model="form.serveStartTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单支付成功时间，用于计算是否进入派单" prop="paySuccessTime">
          <el-date-picker clearable v-model="form.paySuccessTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择订单支付成功时间，用于计算是否进入派单">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="服务数量" prop="purNum">
          <el-input v-model="form.purNum" placeholder="请输入服务数量" />
        </el-form-item>
        <el-form-item label="抢单是否超时" prop="isTimeOut">
          <el-input v-model="form.isTimeOut" placeholder="请输入抢单是否超时" />
        </el-form-item>
        <el-form-item label="抢单列表排序字段" prop="sortBy">
          <el-input v-model="form.sortBy" placeholder="请输入抢单列表排序字段" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSeize, getSeize, delSeize, addSeize, updateSeize } from "@/api/seize/seize";
import axios from 'axios';

export default {
  name: "Seize",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抢单池表格数据
      seizeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 抢单列表排序字段时间范围
      daterangeServeStartTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serveTypeName: null,
        serveAddress: null,
        serveStartTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cityCode: [
          { required: true, message: "城市编码不能为空", trigger: "blur" }
        ],
        serveItemName: [
          { required: true, message: "服务类型不能为空", trigger: "blur" }
        ],
        serveTypeName: [
          { required: true, message: "关键词不能为空", trigger: "blur" }
        ],
        serveAddress: [
          { required: true, message: "服务地址不能为空", trigger: "blur" }
        ],
        serveItemImg: [
          { required: true, message: "服务项目图片不能为空", trigger: "blur" }
        ],
        serveStartTime: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
        purNum: [
          { required: true, message: "服务数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询抢单池列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeServeStartTime && '' != this.daterangeServeStartTime) {
        this.queryParams.params["beginServeStartTime"] = this.daterangeServeStartTime[0];
        this.queryParams.params["endServeStartTime"] = this.daterangeServeStartTime[1];
      }
      listSeize(this.queryParams).then(response => {
        this.seizeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cityCode: null,
        serveTypeId: null,
        serveItemName: null,
        serveTypeName: null,
        serveItemId: null,
        serveAddress: null,
        serveItemImg: null,
        ordersAmount: null,
        serveStartTime: null,
        paySuccessTime: null,
        lon: null,
        lat: null,
        purNum: null,
        isTimeOut: null,
        sortBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeServeStartTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抢单池";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSeize(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSeize(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 抢单按钮 */
    grabOrders(row) {
  // 使用Element UI的确认弹窗
  this.$confirm('是否确认抢单?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 用户点击确定后的逻辑
    this.handleUpdate(row);
    this.handleDelete(row);
  }).catch(() => {
    // 用户点击取消后的逻辑
    // 这里可以不写，因为默认情况下取消不会执行任何操作
  });
},

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      delSeize(ids).then(() => {
        this.getList(); // 刷新列表
        this.$modal.msgSuccess("删除成功"); // 显示删除成功消息
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('seize/seize/export', {
        ...this.queryParams
      }, `seize_${new Date().getTime()}.xlsx`)
    },
    /** 修改操作 */
    handleUpdate(row) {
      const url = 'http://localhost:8080/serve/update';
      // 定义要发送的数据
      const ordersData = {
        id: row.id || this.ids,
        ordersStatus: 200
      };
      // 使用axios发送PUT请求
      axios.put(url, ordersData, {
        params: {
        }
      })
        .then(response => {
          // 请求成功的处理逻辑
          console.log('Response:', response);
        })
        .catch(error => {
          // 请求失败的处理逻辑
          console.error('Error:', error);
        });
    },
  }
};


</script>
